package cn.atguigu;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.util.Properties;

/**
 * @author JiDingXia
 * @date 2025-02-20 20:58:52
 */
public class CustomProducer {
	public static void main(String[] args) {
		// 1. 创建kafka生产者的配置对象
		Properties properties = new Properties();
		// 2. 给kafka配置对象添加配置信息
		properties.put("bootstrap.servers","hadoop102:9092");
		// key,value序列化类
		properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
		properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
		//争对上述的配置信息,可以使用ProducerConfig类提供的枚举值来简化
		//  properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"hadoop102:9092");
		//	properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getClasses());
		//  properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getClasses());
		// 3. 创建kafka生产者对象
		KafkaProducer<String, String> kafkaProducer = new KafkaProducer<String, String>(properties);

		// 4. 调用send方法,发送消息
		for (int i = 0; i < 10; i++) {
				kafkaProducer.send(new ProducerRecord<>("first","kafka" + i));
		}

		// 5. 关闭资源
		kafkaProducer.close();
	}
}
